package com.hdoit.ares.web.cust.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.cust.model.CustCollection;
import com.hdoit.ares.logical.cust.service.ICustCollectionService;
import com.hdoit.ares.logical.cust.service.ICustFootprintService;
import com.hdoit.ares.logical.official.model.Artical;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.file.model.OssFileInfo;
import com.tennetcn.common.logical.file.service.IOssFileInfoService;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;

@Controller
@RequestMapping(value="/api/v1/collectionAndFootprint/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/collectionAndFootprint/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class CollectionAndFootprintAresApis extends BaseApi{

private ICustCollectionService custCollectionService;
	
	public ICustCollectionService getCustCollectionService() {
		return custCollectionService;
	}

	@Resource
	public void setCustCollectionService(
			ICustCollectionService custCollectionService) {
		this.custCollectionService = custCollectionService;
	}

	private ICustFootprintService custFootprintService;
	
	public ICustFootprintService getCustFootprintService() {
		return custFootprintService;
	}
	
	@Resource
	public void setCustFootprintService(ICustFootprintService custFootprintService) {
		this.custFootprintService = custFootprintService;
	}

	private IOssFileInfoService fileInfoService;
	public IOssFileInfoService getFileInfoService() {
		return fileInfoService;
	}
	@Resource
	public void setFileInfoService(IOssFileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}
	
	private IWithdrawParamService withdrawParamService;
	
	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}

	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	private final String collectionApi = "collection";/*我的收藏*/
	private final String footprintApi = "footprint";/*我的足迹*/
	private final String addCollectApi = "addCollect";/*加入收藏*/
	private final String deleteCollectApi = "deleteCollect";/*取消收藏*/
	
	/*分页查询我的收藏中所有内容*/
	@ResponseBody
	@RequestMapping(value = collectionApi)
	@ApiOperation(notes = collectionApi, httpMethod = "POST", value = "我的收藏")
	public String collection(@ApiParam("第几页,必选") @RequestParam(value="pageNum",required=true)Integer pageNum){
		
		ResponseModel rm=new ResponseModel();
		String custId=getLoginId();
		WithdrawParam wp= withdrawParamService.queryModelByHelpCode("pageSize");
		Integer length=wp.getParam();
		List<Artical> articals=custCollectionService.selectAllArtical(custId,pageNum,length);
		if(articals.size()>0){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("ok");
			for(Artical artical:articals){
				String imgurl=artical.getImgUrl();
				/*真实的图片路径前缀*/
				String preUrl = "http://putiyoli-1254043822.cosgz.myqcloud.com";
				/*测试的图片路径前缀*/
				/*String preUrl = "http://test-1254043822.cosgz.myqcloud.com";*/
				if(!StringUtils.isEmpty(imgurl)){
					OssFileInfo ossFileInfo = fileInfoService.queryModel(imgurl);
					artical.setImgUrl(preUrl+ossFileInfo.getFilename());
				}
			}
			List<String> params = new ArrayList<String>();
			Field[] fields = Artical.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			
			rm.putList("artical", articals, params);
		}else{
			rm.setMessage("没有查询到数据");
		}
		return rm.toJson();
	}
	
	/*分页查询我的足迹中所有内容*/
	@ResponseBody
	@RequestMapping(value = footprintApi)
	@ApiOperation(notes = footprintApi, httpMethod = "POST", value = "我的足迹")
	public String footprint(@ApiParam("第几页,必选") @RequestParam(value="pageNum",required=true)Integer pageNum){
		
		ResponseModel rm=new ResponseModel();
		String custId=getLoginId();
		WithdrawParam wp= withdrawParamService.queryModelByHelpCode("pageSize");
		Integer length=wp.getParam();
		List<Artical> articals=custFootprintService.selectAllArtical(custId,pageNum,length);
		if(articals.size()>0){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("ok");
			for(Artical artical:articals){
				String imgurl=artical.getImgUrl();
				/*真实的图片路径前缀*/
				String preUrl = "http://putiyoli-1254043822.cosgz.myqcloud.com";
				/*测试的图片路径前缀*/
				/*String preUrl = "http://test-1254043822.cosgz.myqcloud.com";*/
				if(!StringUtils.isEmpty(imgurl)){
					OssFileInfo ossFileInfo = fileInfoService.queryModel(imgurl);
					artical.setImgUrl(preUrl+ossFileInfo.getFilename());
				}
			}
			List<String> params = new ArrayList<String>();
			Field[] fields = Artical.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			
			rm.putList("artical", articals, params);
		}else{
			rm.setMessage("没有查询到数据");
		}
		
		return rm.toJson();
	}
	
	/*加入收藏*/
	@ResponseBody
	@RequestMapping(value = addCollectApi)
	@ApiOperation(notes = addCollectApi, httpMethod = "POST", value = "加入收藏")
	public String addCollect(@ApiParam("收藏类型,必选") @RequestParam(value="type",required=true)String type,
			@ApiParam("收藏文章或视频的id,必选") @RequestParam(value="thingId",required=true)String thingId){
		
		ResponseModel rm=new ResponseModel();
		String custId=getLoginId();
		CustCollection custCollection=custCollectionService.selectCustCollection(thingId, custId);
		boolean result;
		if(custCollection==null){
			custCollection = new CustCollection();
			custCollection.setId(CommonUtils.getKeyId());
			custCollection.setType(type);
			custCollection.setThingId(thingId);
			custCollection.setCustId(custId);
			custCollection.setCollectionDate(DateUtils.getCurrent());
			
			custCollection.setModelStatus(ModelStatus.add);
			result = custCollectionService.applyChange(custCollection);
		}else{
			rm.setMessage("该内容已收藏,此次收藏失败");
			result=false;
		}
		if(result==true){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("收藏成功");
		}else if(result==false){

		}
		rm.put("result", result);
		
		return rm.toJson();
	}
	
	/*取消收藏*/
	@ResponseBody
	@RequestMapping(value = deleteCollectApi)
	@ApiOperation(notes = deleteCollectApi, httpMethod = "POST", value = "取消收藏")
	public String deleteCollect(@ApiParam("取消收藏的类型,必选") @RequestParam(value="type",required=true)String type,
			@ApiParam("取消收藏的id,必选") @RequestParam(value="thingId",required=true)String thingId){
		
		ResponseModel rm=new ResponseModel();
		String custId=getLoginId();
		int result = custCollectionService.deleteCollection(type, thingId, custId);
		boolean res;
		if(result>0){
			res=true;
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("取消收藏成功");
		}else{
			res=false;
			rm.setMessage("取消收藏失败");
		}
		rm.put("res", res);
		return rm.toJson();
	}
}
